#ifndef CHANNEL_H
#define CHANNEL_H

#include "Common.h"
// #include "EventLoop.h"
#include <functional>
#include <utility>
#include <memory>

class EventLoop;
class Buffer;

#define EVENT_TIMEOUT    0x01
/** Wait for a socket or FD to become readable */
#define EVENT_READ        0x02
/** Wait for a socket or FD to become writeable */
#define EVENT_WRITE    0x04
/** Wait for a POSIX signal to be raised*/
#define EVENT_SIGNAL    0x08



class Channel:noncopyable {
public:
	typedef std::function<void (void *data) > EventReadCallBack;
	typedef std::function<void (void *data) > EventWriteCallBack;

	Channel(int fd, int events, EventLoop *loopPtr):
            fd(fd), events(events), loop(loopPtr), tied_(false), data(nullptr) {
    }

    ~Channel() {

    }

    void setEventReadCallBack(const EventReadCallBack &cb) {
    	eventReadCallBack = std::move(cb);
    }

    void setEventWriteCallBack(const EventWriteCallBack &cb) {
    	eventWriteCallBack = std::move(cb);
    }

    int writeEventIsEnable(void) {
    	return events & EVENT_WRITE;
    }

    void tie(const std::shared_ptr <void> &obj) {
        tie_ = obj;
        tied_ = true;
    }

    void add(int fd);
    void remove(int fd);
    void writeEventDisable(void);
    void writeEventEnable(void);

public:

    EventLoop *loop;
	int fd;
	int	events;
    std::weak_ptr<void> tie_;
    bool tied_;
    EventReadCallBack eventReadCallBack;
    EventWriteCallBack eventWriteCallBack;
	void *data;
    int type;
};

#endif